package com.vains.handler;

import com.vains.bean.Result;
import com.vains.bean.exception.GatewayException;
import com.vains.constant.HttpStatusConstants;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author vains
 * @date 2021/3/11 16:35
 */
@RestControllerAdvice
public class CommonExceptionHandler {

    /**
     * 运行时异常
     * @param e 异常实例
     * @return Result
     */
    @ExceptionHandler(RuntimeException.class)
    public Result<String> runtimeException(RuntimeException e) {
        return Result.error(e.getMessage());
    }

    /**
     * 运行时异常
     * @param e 异常实例
     * @return Result
     */
    @ExceptionHandler(GatewayException.class)
    public Result<String> gatewayException(GatewayException e) {
        return Result.error(HttpStatusConstants.BAD_GATEWAY, e.getMessage());
    }

}
